﻿#pragma once

#include "ccamera.h"

#include "pylon/PylonIncludes.h"
#include "pylon/gige/BaslerGigEInstantCamera.h"
#include "pylon/gige/BaslerGigEImageEventHandler.h"

#include "GenApi/Container.h"
#include "pylon/CameraEventHandler.h"
#include <QVector>

namespace Gen {

typedef Pylon::CBaslerGigEInstantCamera Camera_t;
typedef Pylon::CBaslerGigECameraEventHandler CameraEventHandler_t;

class CBaslerGigeCamera;


/*!
 * \brief The CBaslerGigeCameraDriver class
 */
class CBaslerGigeCameraDriver: public ICameraDriver,
    public Pylon::CImageEventHandler                           // For receiving grabbed images.
{
public:
    CBaslerGigeCameraDriver();
    QStringList cameraNameList()const;
    ICamera* cameraInstance(const QString& name);
private:
    QStringList m_nameList;
    Pylon::DeviceInfoList_t m_devList;
};

/*!
 * \brief The CBaslerGigeCamera class
 */
class CBaslerGigeCamera: public ICamera
    , public Pylon::CImageEventHandler
    , public Pylon::CCameraEventHandler
{
    Q_OBJECT
public:
    CBaslerGigeCamera(const Pylon::CDeviceInfo& deviceInfo);
    ~CBaslerGigeCamera();
    bool open() override;
    bool isOpened() const override;
    void close() override;
    void destroyDevice() override;

    int gain() const override;
    void setGain(int value) override;
    int maxGain() const override;
    int minGain() const override;

    int exposure() const override;
    void setExposure(int value) override;
    int maxExposure() const override;
    int minExposure() const override;

    TriggerMode triggerMode() const override;
    void setTriggerMode(TriggerMode triggermode = Software) override;

    bool blockGrabOne(int timeout) override;
    bool grabOne() override;
    bool grabContinue() override;
    bool grabStop() override;
    bool isContinue() const override;
    bool setCameraEvent();
    bool setFrameRate(float value) override;
    virtual void OnImageGrabbed(Pylon::CInstantCamera& camera, const Pylon::CGrabResultPtr& ptrGrabResult) override;
//    virtual void OnCameraEvent(Pylon::CInstantCamera& camera, intptr_t userProvidedId, GenApi::INode* );
    virtual QImage image() const override;
private:
    Pylon::CDeviceInfo m_deviceInfo;
    Camera_t* m_camera;
    GenApi::INodeMap* m_nodeMap;
    TriggerMode m_triggerMode;
    QVector<uchar> m_buffer;
    int m_width;
    int m_height;
};


}
